package cn.aliothstar.code;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @作者：玉蘅
 * @项目名称：sever-synchronization
 * @包名：cn.aliothstar.code
 * @文件名称：One
 * @代码功能：从TXT文件读取
 * @时间：2023/09/25/20:55
 */
public class One {
    private static int checkCount = 0; // 计数器

    public static void main(String[] args) {
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new CheckServersTask(), 0, 10000); // 每隔10秒执行一次任务
    }

    private static class CheckServersTask extends TimerTask {
        @Override
        public void run() {
            checkCount++; // 增加检测次数
            System.out.println("+++++++++++++++++++++++++++++");
            System.out.println("第 " + checkCount + " 次检查");
            System.out.println("+++++++++++++++++++++++++++++");
            try (BufferedReader reader = new BufferedReader(new FileReader("resource/server.txt"))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    line = line.trim();
                    if (!line.isEmpty()) {
                        System.out.println("检查服务器: " + line);
                        if (isValidIPAddress(line)) {
                            System.out.println("这是一个IP地址.");
                            checkServer(line, "http://" + line);
                            checkServer(line, "https://" + line);
                        } else if (line.startsWith("http://")) {
                            System.out.println("这是一个HTTP地址.");
                            checkServer(line, line);
                        } else if (line.startsWith("https://")) {
                            System.out.println("这是一个HTTPS地址.");
                            checkServer(line, line);
                        } else {
                            System.out.println("无效的服务器格式: " + line);
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        private boolean isValidIPAddress(String address) {
            try {
                InetAddress.getByName(address);
                return true;
            } catch (Exception e) {
                return false;
            }
        }

        private void checkServer(String serverName, String serverURL) {
            try {
                long startTime = System.currentTimeMillis();
                URL url = new URL(serverURL);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                int responseCode = connection.getResponseCode();
                long endTime = System.currentTimeMillis();
                long elapsedTime = endTime - startTime;

                System.out.println("服务器: " + serverName);
                System.out.println("响应码: " + responseCode);
                System.out.println("响应时间: " + elapsedTime + " ms");

                if (responseCode == HttpURLConnection.HTTP_OK) {
                    System.out.println("服务器正常.");
                    System.out.println("--------------------------------");
                } else {
                    System.out.println("无法访问服务器.");
                }
            } catch (IOException e) {
                System.out.println(e.getMessage());
            }
        }
    }
}

